home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / CBASE102.ARJ / CBKEYALI.C < prev    next >
Text File  |  1991-09-23  |  3KB  |  159 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbkeyali.c    1.5 - 91/09/23" */
  5.  
  6. #include <ansi.h>
  7.  
  8. /* ansi headers */
  9. #include <errno.h>
  10. #ifdef AC_STDLIB
  11. #include <stdlib.h>
  12. #endif
  13. #ifdef AC_STRING
  14. #include <string.h>
  15. #endif
  16.  
  17. /* library headers */
  18. #include <blkio.h>
  19. #include <btree.h>
  20. #include <lseq.h>
  21.  
  22. /* local headers */
  23. #include "cbase_.h"
  24.  
  25. /*man---------------------------------------------------------------------------
  26. NAME
  27.      cbkeyalign - align cbase key cursor
  28.  
  29. SYNOPSIS
  30.      #include <cbase.h>
  31.  
  32.      int cbkeyalign(cbp, field)
  33.      cbase_t *cbp;
  34.      int field;
  35.  
  36. DESCRIPTION
  37.      The cbkeyalign function aligns a key cursor in cbase cbp with the
  38.      record cursor.  The key cursor is positioned to the key
  39.      associated with the current record.  Other cursors are not
  40.      affected.
  41.  
  42.      cbkeyalign will fail if one or more of the following is true:
  43.  
  44.      [EINVAL]       cbp is not a valid cbase pointer.
  45.      [EINVAL]       field is not a valid field number for
  46.                     cbase cbp.
  47.      [CBELOCK]      cbp is not locked.
  48.      [CBENKEY]      field is not a key.
  49.      [CBENOPEN]     cbp is not open.
  50.  
  51. SEE ALSO
  52.      cbkeyfirst, cbkeylast, cbkeynext, cbkeyprev, cbkeysrch.
  53.  
  54. DIAGNOSTICS
  55.      Upon successful completion, a value of 0 is returned.  Otherwise,
  56.      a value of -1 is returned, and errno set to indicate the error.
  57.  
  58. ------------------------------------------------------------------------------*/
  59. #ifdef AC_PROTO
  60. int cbkeyalign(cbase_t *cbp, int field)
  61. #else
  62. int cbkeyalign(cbp, field)
  63. cbase_t *cbp;
  64. int field;
  65. #endif
  66. {
  67.     void *        buf    = NULL;
  68.     cbrpos_t    cbrpos    = NIL;
  69.     int        found    = 0;
  70.     lspos_t        lspos    = NIL;
  71.  
  72.     /* validate arguments */
  73.     if (!cb_valid(cbp)) {
  74.         errno = EINVAL;
  75.         return -1;
  76.     }
  77.  
  78.     /* check if not open */
  79.     if (!(cbp->flags & CBOPEN)) {
  80.         errno = CBENOPEN;
  81.         return -1;
  82.     }
  83.  
  84.     /* validate arguments */
  85.     if (field < 0 || field >= cbp->fldc) {
  86.         errno = EINVAL;
  87.         return -1;
  88.     }
  89.  
  90.     /* check if field not a key */
  91.     if (!(cbp->fldv[field].flags & CB_FKEY)) {
  92.         errno = CBENKEY;
  93.         return -1;
  94.     }
  95.  
  96.     /* check if not locked */
  97.     if (!(cbp->flags & CBLOCKS)) {
  98.         errno = CBELOCK;
  99.         return -1;
  100.     }
  101.  
  102.     /* check if record cursor is null */
  103.     if (lscursor(cbp->lsp) == NULL) {
  104.         /* set key cursor to null */
  105.         if (btsetcur(cbp->btpv[field], NULL) == -1) {
  106.             CBEPRINT;
  107.             return -1;
  108.         }
  109.         return 0;
  110.     }
  111.  
  112.     /* allocate storage for key */
  113.     if (btkeysize(cbp->btpv[field]) != (cbp->fldv[field].len + sizeof(cbrpos_t))) {
  114.         CBEPRINT;
  115.         errno = CBEPANIC;
  116.         return -1;
  117.     }
  118.     buf = calloc((size_t)1, btkeysize(cbp->btpv[field]));
  119.     if (buf == NULL) {
  120.         CBEPRINT;
  121.         errno = ENOMEM;
  122.         return -1;
  123.     }
  124.  
  125.     /* get record field */
  126.     if (cbgetrf(cbp, field, buf) == -1) {
  127.         CBEPRINT;
  128.         free(buf);
  129.         return -1;
  130.     }
  131.  
  132.     /* get record position and place in key */
  133.     if (lsgetcur(cbp->lsp, &lspos) == -1) {
  134.         CBEPRINT;
  135.         free(buf);
  136.         return -1;
  137.     }
  138.     cbrpos = lspos;
  139.     memcpy((char *)buf + cbp->fldv[field].len, &cbrpos, sizeof(cbrpos_t));
  140.  
  141.     /* position key cursor */
  142.     found = btsearch(cbp->btpv[field], buf);
  143.     if (found == -1) {
  144.         CBEPRINT;
  145.         free(buf);
  146.         return -1;
  147.     }
  148.     if (found == 0) {
  149.         free(buf);
  150.         errno = CBECORRUPT;
  151.         return -1;
  152.     }
  153.  
  154.     free(buf);
  155.     buf = NULL;
  156.  
  157.     return 0;
  158. }
  159.